package com.lrkj.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.lrkj.back.entity.TbAffiche;
import com.lrkj.back.service.TbAfficheService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/affiche")
public class TbAfficheController  extends BaseController{
	@Autowired
	private TbAfficheService afficheSerice;

	//首页--通知公告    （点击查询）
	@RequestMapping("/afficheList.do")
	public ModelAndView packageingList(String searchTxt,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("afficheTitle", searchTxt.trim());//字段查询
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<TbAffiche> page = afficheSerice.findAllTbAffiche(map);
		request.setAttribute("afficheList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/system/gonggaoxinxi/affiches_list");
	}


	//点添加和修改按纽用到的方法
	@RequestMapping("/afficheEdit.do")
	public ModelAndView afficheEdit(Integer id, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		TbAffiche affiche = (id == null) ? new TbAffiche() : afficheSerice.selectByPrimaryKey(id);
		request.setAttribute("affiche", affiche);
		return new ModelAndView("/system/gonggaoxinxi/affiches_edit");
	}
	//修改和保存的方法
	@RequestMapping("/afficheSave.do")
	@ResponseBody
	public Map<Object,Object> afficheSave(TbAffiche affiche){
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			affiche.setAfficheDate(new Date());
			if(affiche.getAfficheId() != null){
				afficheSerice.updateByPrimaryKey(affiche);
			}else{
				afficheSerice.insert(affiche);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return  rtnMap;
	}
	//删除
	@RequestMapping("/afficheDel.do")
	@ResponseBody
	public Map<Object,Object> afficheDel(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				afficheSerice.deleteByPrimaryKey(Integer.valueOf(tid));
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
    @RequestMapping("findOne.do")
    public ModelAndView findOne(String id, HttpServletRequest request, HttpServletResponse response) throws BusinessException{
    	TbAffiche affiche = null;
    	//i7 8700k i7 6700
    	if (StringUtils.isNotBlank(id)) {
    		affiche = afficheSerice.selectByPrimaryKey(Integer.valueOf(id));
		}
    	if (affiche != null) {
			request.setAttribute("affiche",affiche);
		}
    	return new ModelAndView("/system/gonggaoxinxi/affiche_xiangqing");
    }
}
